package com.ci.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ChoseEmulator {

	public static void main(String[] args) throws InterruptedException, IOException {
		ChoseEmulator ch = new ChoseEmulator();
		String dev=ch.getIdleDevices();
		Runtime.getRuntime().exec("adb -s "+dev+" shell uiautomator runtest SmokeTest.jar --nohup -c com.lenovo.uitest.lelauncher.LeLauncherCase_V5_0_fan#testBackup"
);
		Thread.sleep(8000);
		ch.killUiautomator(dev);
		System.out.println("----------------------------------------");
	}

	/**
	 * 需求：获取一个空闲的设置，不在运行uiautomator，返回一个设备号
	 * 
	 * @return
	 */
	public String getIdleDevices() {

		String device = this.getExecCmdStr("adb devices");
		String[] dev = this.splitDevice(device);
		boolean isUiRun = false;
		for (int i = 0; i < dev.length; i++) {
			isUiRun = this.isRunUiautomator(dev[i]);
			if (!isUiRun) {
				return dev[i];
			}

		}
		return "无空闲设备";
	}

	/**
	 * 需求：结束某个设备的uiautomator的运行
	 * 
	 * @param dev
	 */
	public void killUiautomator(String dev) {
		// shell 26377 26375 455692 20488 ffffffff 40082e20 S uiautomator,
		// adb shell kill 26377
		String cmd = "adb -s " + dev + " shell ps | grep uiautomator";
		String execString = this.getExecCmdStr(cmd);
		if (!execString.equals("")) {
			String[] p = execString.split(" ");
			for (int i = 0; i < p.length; i++) {
				if (p[i].matches(".*\\d.*")) {
					//System.out.println("uiautomator进程：" + p[i]);
					this.execCmd("adb -s " + dev + " shell kill " + p[i]);
					return;
				}
			}
		}

	}

	/*
	 * ***********************************
	 * 以下为中间辅助函数 
	 * **************************************
	 */

	/**
	 * 需求：判断某个设备是否处于运行uiautomator的状态中
	 * 
	 * @param dev
	 * @return
	 */
	public boolean isRunUiautomator(String dev) {
		String cmd = "adb -s " + dev + " shell ps | grep uiautomator";
		String execString = this.getExecCmdStr(cmd);
		//System.out.println("返回值-" + execString + "-");
		return execString.contains("uiautomator");
	}

	/**
	 * 需求：运行命令，且返回命令输出后的系统返回值
	 * 
	 * @param cmd
	 * @return
	 */
	public String getExecCmdStr(String cmd) {
		String line = "";
		String out = "";
		try {
			Process p = Runtime.getRuntime().exec(cmd);
			InputStream input = p.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					input));

			while ((line = reader.readLine()) != null) {
				// System.out.println(line);
				out = out + line + ",";
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		return out;
	}

	/**
	 * 需求：解析拆分成单独的设备号
	 * 
	 * @param devide
	 * @return
	 */
	public String[] splitDevice(String devide) {
		String[] s = devide.split(",");
		int l = s.length;
		String[] devices = new String[l - 1];
		int sit = 0;
		for (int i = 0; i < devices.length; i++) {
			sit = s[i + 1].indexOf("device");
			devices[i] = s[i + 1].substring(0, sit - 1);
		}
		return devices;
	}

	/**
	 * 需求：运行系统命令
	 * 
	 * @param cmd
	 */
	public void execCmd(String cmd) {
		System.out.println("----execCmd:  " + cmd);
		try {
			Process p = Runtime.getRuntime().exec(cmd);
			InputStream input = p.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					input));
			String line = "";
			while ((line = reader.readLine()) != null) {
				System.out.println(line);
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
